// src/api/platform.ts
import axios from 'axios'

// 创建axios实例
const apiClient = axios.create({
  baseURL: '/api/v1', // 这里应该是实际的后端API地址
  timeout: 10000,
  headers: {
    'Content-Type': 'application/json'
  }
})

// 请求拦截器
apiClient.interceptors.request.use(
  (config) => {
    // 在发送请求之前做些什么，比如添加token
    const token = localStorage.getItem('platform-admin-token')
    if (token) {
      config.headers.Authorization = `Bearer ${token}`
    }
    return config
  },
  (error) => {
    return Promise.reject(error)
  }
)

// 响应拦截器
apiClient.interceptors.response.use(
  (response) => {
    return response.data
  },
  (error) => {
    if (error.response?.status === 401) {
      // 处理未授权错误，比如跳转到登录页
      localStorage.removeItem('platform-admin-token')
      window.location.href = '/login'
    }
    return Promise.reject(error)
  }
)

// 平台概览相关API
export const platformAPI = {
  // 获取系统监控数据
  getSystemMonitorData: () => {
    return apiClient.get('/dashboard/system-monitor')
  },
  
  // 获取运营数据
  getOperationData: () => {
    return apiClient.get('/dashboard/operation-data')
  },
  
  // 获取服务状态
  getServiceStatus: () => {
    return apiClient.get('/dashboard/service-status')
  }
}

// 租户管理相关API
export const tenantAPI = {
  // 获取组织列表
  getOrganizations: (params?: any) => {
    return apiClient.get('/tenant/organizations', { params })
  },
  
  // 创建组织
  createOrganization: (data: any) => {
    return apiClient.post('/tenant/organizations', data)
  },
  
  // 更新组织
  updateOrganization: (id: number, data: any) => {
    return apiClient.put(`/tenant/organizations/${id}`, data)
  },
  
  // 删除组织
  deleteOrganization: (id: number) => {
    return apiClient.delete(`/tenant/organizations/${id}`)
  },
  
  // 获取用户列表
  getUsers: (params?: any) => {
    return apiClient.get('/tenant/users', { params })
  },
  
  // 创建用户
  createUser: (data: any) => {
    return apiClient.post('/tenant/users', data)
  }
}

// 财务管理相关API
export const financeAPI = {
  // 获取收入数据
  getIncomeData: (params?: any) => {
    return apiClient.get('/finance/income', { params })
  },
  
  // 获取费用配置
  getCostConfig: () => {
    return apiClient.get('/finance/cost')
  },
  
  // 更新费用配置
  updateCostConfig: (data: any) => {
    return apiClient.put('/finance/cost', data)
  }
}

// 系统运维相关API
export const systemAPI = {
  // 获取服务监控数据
  getServiceMonitorData: () => {
    return apiClient.get('/system/monitor')
  },
  
  // 获取日志数据
  getLogs: (params?: any) => {
    return apiClient.get('/system/logs', { params })
  }
}

// 运营支持相关API
export const supportAPI = {
  // 获取工单列表
  getTickets: (params?: any) => {
    return apiClient.get('/support/tickets', { params })
  },
  
  // 创建工单
  createTicket: (data: any) => {
    return apiClient.post('/support/tickets', data)
  },
  
  // 更新工单状态
  updateTicket: (id: number, data: any) => {
    return apiClient.put(`/support/tickets/${id}`, data)
  }
}

// 安全管理相关API
export const securityAPI = {
  // 获取角色列表
  getRoles: () => {
    return apiClient.get('/security/roles')
  },
  
  // 创建角色
  createRole: (data: any) => {
    return apiClient.post('/security/roles', data)
  },
  
  // 获取权限列表
  getPermissions: () => {
    return apiClient.get('/security/permissions')
  },
  
  // 更新权限配置
  updatePermissions: (data: any) => {
    return apiClient.put('/security/permissions', data)
  }
}

// 认证相关API
export const authAPI = {
  // 登录
  login: (credentials: { username: string; password: string }) => {
    return apiClient.post('/auth/login', credentials)
  },
  
  // 登出
  logout: () => {
    return apiClient.post('/auth/logout')
  },
  
  // 获取当前用户信息
  getCurrentUser: () => {
    return apiClient.get('/auth/current-user')
  }
}

export default apiClient